--
-- Author: ping.wang
-- Date: 2018-09-13 11:31:12
--
-- UniversalShopPanel
-- 通用的商店panel

local ClassRef = app.mvc.createPanelClass(...)
local IconRender = require("app.widget.renders.IconRender")
local ListViewVertical = require("app.widget.listview.ListViewVertical")
local PrestigeShopRender = import(".renders.PrestigeShopRender")

local table_insert = table.insert
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.UniversalShopNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._model = app:getInst("PrestigeShopModel")
    -- -- 初始化已开启声望商店列表(每次打开界面都重新初始化一次)
    -- self._model:initPrestigeShopList()

    self._topPanel = self._ui.Panel_top
    self._panel_achieve = self._ui.Panel_achieve
    self._panel_renown = self._ui.Panel_renown
    self._panel_needGuild = self._ui.Panel_guild
    self._panel_supplies = self._panel_renown:getChildByName("Panel_supplies")
    self._panel_num = self._panel_renown:getChildByName("Panel_num")
    self._panel_lack = self._panel_renown:getChildByName("Panel_lack")
    self._btnBuy = self._panel_renown:getChildByName("Button_buy")
    self._btnLook = self._panel_renown:getChildByName("btn_look")
    self._originalX = self._btnBuy:getPositionX()
    self._originalMidX = self._panel_renown:getContentSize().width / 2
    self._panel_condition = self._ui.Panel_condition -- 一个商品可多个道具购买
    self._condition_btnBuy = self._panel_condition:getChildByName("Button_buy")
    self._condition_btnLook = self._panel_condition:getChildByName("btn_look")
    self._condition_originalX = self._condition_btnBuy:getPositionX()
    self._condition_originalMidX = self._panel_condition:getContentSize().width / 2
    self._achieve_btnBuy = self._panel_achieve:getChildByName("Button_buy")
    self._achieve_btnLook = self._panel_achieve:getChildByName("btn_look")
    self._achieve_originalX = self._achieve_btnBuy:getPositionX()
    self._achieve_originalMidX = self._panel_achieve:getContentSize().width / 2

    self._topPanel:setVisible(false)
    self._panel_achieve:setVisible(false)
    self._panel_renown:setVisible(false)
    self._panel_condition:setVisible(false)
    self._shopName = self._ui["Panel/Panel_tlt/tlt"]
    self._panel_tips = self._ui["Panel/Panel_tips"]
    self._panel_tips:setVisible(false)

    -- left panel
    self._ui.ListView:setVisible(false)
    self._menuBarPanel = self._ui.Panel_achieve_L
    self._prePageBtn = self._menuBarPanel:getChildByName("Button_1")
    self._nextPageBtn = self._menuBarPanel:getChildByName("Button_2")
    self._dotPanel = self._menuBarPanel:getChildByName("Panel_dot")
    self._dotoffx = self._dotPanel:getChildByName("dot_2"):getPositionX() - self._dotPanel:getChildByName("dot_1"):getPositionX()
    self._dotColor = self._dotPanel:getChildByName("dot_1"):getColor()
    self._btnPanel = self._menuBarPanel:getChildByName("Panel_btn")
    self._menuBarPanel:setVisible(false)
    self._prePageBtn:addClickEventListener(function()
        if self._curPage > 1 then
            self._curPage = self._curPage - 1
            self._curIdx = 1 -- 切页时都默认选中第一个
            self:_initMenuBar()
            self._shopId = self._model:getShopIdByIndex(self._curPage, self._curIdx)
            self:updateLeftPanel()
        end
    end)
    self._nextPageBtn:addClickEventListener(function()
        if self._curPage < self._countPage then
            self._curPage  = self._curPage + 1
            self._curIdx = 1 -- 切页时都默认选中第一个
            self:_initMenuBar()
            self._shopId = self._model:getShopIdByIndex(self._curPage, self._curIdx)
            self:updateLeftPanel()
        end
    end)
    for i = 1 , 5 do
        local nameNode = self._btnPanel:getChildByName("name_"..i)
        nameNode:setTouchEnabled(true)
        nameNode:addTouchEventListener(function(sender, eventName)
            if eventName == ccui.TouchEventType.ended then
                self._curIdx = i
                self:_refreshSelectedTag()
                self._shopId = self._model:getShopIdByIndex(self._curPage, self._curIdx)
                self:updateLeftPanel()
            end
        end)
    end
    self:_initScrollView()

    self:setContentSize( self._ui.Panel:getContentSize() )
    self:refresh()

    self:registerViewEvent({
        {name = ViewEvent.Shop_onQueryGoodsSuccess, callback = handler(self, self.onQueryGoodsSuccess)},
        {name = ViewEvent.Shop_onBuyGoodsSuccess, callback = handler(self, self.onBuyGoodsSuccess)},
    })

    self._itemInfo = nil -- 默认选中的商品 
    self._isRequestData = false -- 是否在请求服务器数据的过程中
    self._isPrestigeShop = false -- 是否是从声望商店进入的
end

function ClassRef:initPanel(data)
    self._isPrestigeShop = true
    self._curTabIndex = data and data.tabIndex -- 从声望商店进入时，选中的侧边TabIndex
    self._curIdx = data and data.index -- 声望商店选中的页签
    self._curPage = data and data.page -- 声望商店菜单栏当前页，默认第一页
    self._countPage = self._model:getCountPage() -- 声望商店菜单栏总页数
    self._shopList = nil -- 声望商店当前选中页的商店列表
    self._shopId = data and data.shopId -- 商店Id
    self:setSelectItemInfo({data.itemType, data.itemId})
    self:_initMenuBar()
    self:updateLeftPanel()
end

-- 初始化商品列表节点
function ClassRef:_initScrollView()
    local params = {
        renderClass = PrestigeShopRender,
        perNum = 2,
        cellEventProxy = function(eventName, cellModel)
			if eventName == "click" then
				self:_onChangeSelectedGoods(cellModel:getIndex())
			end
		end,
    }
    self._sv1 = ListViewVertical.new(self._menuBarPanel:getChildByName("ScrollView_1"), params)
    self._sv2 = ListViewVertical.new(self._ui.ScrollView, params)
end

-- 初始化声望商店菜单栏节点
function ClassRef:_initMenuBar()
    self._shopList = self._model:getShopsByPage(self._curPage)
    local countWidth = 0 -- 所有标题的总长度
    local panelWidth = self._btnPanel:getContentSize().width
    local count = 0 -- 总共有多少个标题
    local offset = 0 -- 每个标题之间的间隔
    local px = 0 -- 标题的位置
    for i = 1, 5 do
        local nameNode = self._btnPanel:getChildByName("name_"..i)
        if self._shopList[i] then
            nameNode:setVisible(true)
            nameNode:setString(self._shopList[i].name)
            countWidth = countWidth + nameNode:getVirtualRendererSize().width
            count = count + 1
        else
            nameNode:setVisible(false)
        end
    end
    offset = count >= 5 and (panelWidth - countWidth)/4 or 40
    -- 设置每个标题的位置
    for i = 1, count do
        local nameNode = self._btnPanel:getChildByName("name_"..i)
        px = px + nameNode:getVirtualRendererSize().width/2
        nameNode:setPositionX(px)
        px = px + nameNode:getVirtualRendererSize().width/2 + offset
    end
    self:_refreshSelectedTag()
    self:_initPageDot()
end

-- 初始化页数节点
function ClassRef:_initPageDot()
    for i = 1, 4 do
        local dotNode = self._dotPanel:getChildByName("dot_"..i)
        local pWidth = dotNode:getContentSize().width*self._countPage+self._dotoffx*(self._countPage-1)
        if i <= self._countPage then
            local px = (self._dotPanel:getContentSize().width - pWidth) / 2 + (i - 1) * (dotNode:getContentSize().width+self._dotoffx)
            dotNode:setPositionX(px)
            dotNode:setVisible(true)
        else
            dotNode:setVisible(false)
        end
        if i == self._curPage then
            dotNode:setColor(cc.c3b(255,255,255))
        else
            dotNode:setColor(cc.c3b(self._dotColor.r,self._dotColor.g,self._dotColor.b))
        end
    end
end

function ClassRef:_refreshSelectedTag()
    for i = 1, 5 do
        local nameNode = self._btnPanel:getChildByName("name_"..i)
        if self._curIdx == i then
            nameNode:setOpacity(255)
        else
            nameNode:setOpacity(255*0.45)
        end
    end
end

-- 选中的右侧tab
function ClassRef:setSelectedIndex(index)
    if self._curTabIndex and self._curTabIndex == index then
        return
    end
    self._curTabIndex = index
    if self._curTabIndex == 1 then
        self:_initMenuBar()
        local index = self._curIdx
        self._shopId = self._model:getShopIdByIndex(self._curPage, index)
    elseif self._curTabIndex == 2 then
        self._shopId = 72051
    elseif self._curTabIndex == 3 then
        self._shopId = 72071
    end
    self:updateLeftPanel()
end

-- 请求数据更新左侧面板
function ClassRef:updateLeftPanel()
    if not self._isRequestData then
        self._model:reqPrestigeShopGoods(self._shopId)
        if not self._isPrestigeShop then
            self._isRequestData = true
        end
    end
end

-- 设置商店Id
function ClassRef:setShopId(id)
    self._shopId = id
end

-- 选中道具
function ClassRef:setSelectItemInfo(params)
    self._itemInfo = params
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

function ClassRef:updateShopItem(goods)
    self._sv:foreachVisibleCellModels(function(cellModel, data)
        if cellModel.cell and data.id == goods.id then
            if goods.numType == "DayLimit" then
                local conS = string.format(L("text_12200010"),goods.leftNum,goods.limitNum)
                cellModel.cell:setConDesNode(conS)
            elseif goods.numType == "WeeklyLimit" then
                local conS = string.format(L("text_12200011"),goods.leftNum,goods.limitNum)
                cellModel.cell:setConDesNode(conS)
            end
        end
    end)
end

function ClassRef:setCurrencyIcon(iconNode, goods)
    iconNode:setVisible(false)
    local isMultiCost = self._model:isMultiCost(goods)
    if not isMultiCost then
        local itemData = UD:parseLootToBagItem(goods.cost[1])
        if itemData and itemData.getIcon then
            iconNode:setVisible(true)
            iconNode:loadTexture(itemData:getIcon())
        end
    end
end

function ClassRef:setAchieveShopItemStatus(goods, conditionStatus, siteConditionStr)
    -- 成就商店
    local achievePanel = self._panel_achieve:getChildByName("Panel_num")
    local achieveDot = achievePanel:getChildByName("dot")
    local achievePriceText = achievePanel:getChildByName("num")
    local achieveGoldIcon = achievePanel:getChildByName("gold")
    local achieveText = self._panel_achieve:getChildByName("text")

    self:setCurrencyIcon(achieveGoldIcon, goods)
    achievePriceText:setString(goods.price)
    display.setNodesMiddle(achievePanel, achieveDot, achievePriceText, achieveGoldIcon)
    achieveText:setString(siteConditionStr)
    if conditionStatus then
        achieveText:setColor(cc.c3b(0xff, 0xe1, 0x84))
    else
        achieveText:setColor(cc.c3b(255, 105, 85))
    end
    display.setBtnTouchEnable(self._achieve_btnBuy, conditionStatus)
end

function ClassRef:refreshItemStatus(goods)
   if not goods then return end
    
    self._topPanel:setVisible(true)
    local nameNode = self._topPanel:getChildByName("name")
    local desNode = self._topPanel:getChildByName("text")
    local typeNode = self._topPanel:getChildByName("tips_1")
    local bindTypeNode = self._topPanel:getChildByName("tips_2")
    local iconRoot = self._topPanel:getChildByName("icon")

    local render = IconRender.create(iconRoot)
    render:setData(goods.fakeItemData or goods.itemData)
    render:setTipEnable(true)
    render:setTipUnShowOrigin(true)
    render:setComparedTipEnabled(true)

    local bind = 99 -- 默认不显示
    local typeName = ""
    local name = ""
    local iconPath
    local description = ""
    if goods.itemType == "equip" then
        local equipConfig = GD:getEquipConf(goods.itemId)
        if equipConfig then
            name = L(equipConfig.string)
            description = L(equipConfig.des)
            bind = checknumber(equipConfig.bind)
        end

        typeName = L("text_10400002")

        local equipRes = GD:getEquipResConf(goods.itemId)
        if equipRes and equipRes.icon then
            iconPath = GD:formatIconFilePath(Enums.ItemBagType.equip, equipRes.icon)
        end
    elseif goods.itemType == "item" then
        local itemConfig = GD:queryItemById(goods.itemId)
        typeName = L(Res.showTypeName .. itemConfig.showType)
        name = L(itemConfig.name)
        iconPath = GD:formatIconFilePath(Enums.ItemBagType.item, itemConfig.icon)
        description = L(itemConfig.desc)
        bind = checknumber(itemConfig.bind)
    else
        --其他所有类型通过此来读
        local mountSoul = goods.fakeItemData or goods.itemData
        typeName = mountSoul:getTypeName()
        name = mountSoul:getName(true)
        iconPath = mountSoul:getIcon()
        description = mountSoul:getDesc()
    end
    if goods:getDesc() ~= "" then
        description = goods:getDesc() -- 如果goods表有描述，用goods的描述，否则用商品本身的描述
    end
    local bindString = ""
    if bind == 0 then
        bindString = L("tradable")
    elseif bind == 1 then
        bindString = L("lua_code_text_296")
    elseif bind == 2 then
        bindString = L("lua_code_text_320")
    else
        -- 不显示 donothing
    end
    bindTypeNode:setString(bindString)
    typeNode:setString(typeName)
    nameNode:setString(name)
    desNode:setString(description)

    self._panel_condition:setVisible(false)
    self._panel_achieve:setVisible(false)
    self._panel_supplies:setVisible(false)
    self._panel_num:setVisible(false)
    self._panel_lack:setVisible(false)
    self._panel_needGuild:setVisible(false)

    local isAchieveShop = self._shopId == 72051 -- 是否是成就商店
    local checkGuild = self._shopId == 72071 and not UD:isInGuild() or false -- 是否在公会中
    local conditionStatus,conditionStr,iconPath,siteConditionStr = self._model:getConditionStatusByGoods(goods)
    local isMultiCost = self._model:isMultiCost(goods)
    local btnBuy = isAchieveShop and self._achieve_btnBuy or self._btnBuy
    local btnLook = isAchieveShop and self._achieve_btnLook or self._btnLook
    local originalX = isAchieveShop and self._achieve_originalX or self._originalX
    local originalMidX = isAchieveShop and self._achieve_originalMidX or self._originalMidX
    if checkGuild then
        self._panel_needGuild:setVisible(true)
    elseif conditionStatus then
        if isMultiCost then --多个道具购买
            btnBuy = self._condition_btnBuy
            btnLook = self._condition_btnLook
            originalX = self._condition_originalX
            originalMidX = self._condition_originalMidX
            for i = 1, 3 do
                local name = "icon_"..i
                local iconNode = self._panel_condition:getChildByName(name)
                if iconNode then
                    iconNode:setVisible(false)
                end 
            end
            local uWidth = self._panel_condition:getContentSize().width
            local costData = self._model:transLootToItemData(goods)
            for i, item in ipairs(costData) do
                local name = "icon_"..i
                local iconNode = self._panel_condition:getChildByName(name)
                if iconNode then
                    iconNode:setVisible(true)
                    local uiItem = IconRender.create(iconNode)
                    uiItem:setData(item)
			        uiItem:setTipEnable(true)
			        uiItem:setNumBgVisible(true)
                    local hasNum = UD:getNumByRenderData(item)
                    local isEnough = hasNum >= item.amount
                    uiItem:setNumString(hasNum .. "/" .. item.amount)
                    if not isEnough then
                        uiItem:setNumberColor(display.COLOR_RED)
                    else
                        uiItem:setNumberColor(display.COLOR4_GREEN)
                    end
                    uiItem:updateNumberSize()
                    local sWidth = iconNode:getContentSize().width + 10
                    local posY = iconNode:getPositionY()
                    local sx = (uWidth - #goods.cost * sWidth) / 2 + (i - 1) * sWidth + 5
			        uiItem:setPosition(cc.p(sx,posY))
                end
            end
            self:updateGoodsCount(goods)
        else
            if isAchieveShop then
                self:setAchieveShopItemStatus(goods, conditionStatus, siteConditionStr)
            else
                self._panel_supplies:setVisible(true)
                self._panel_num:setVisible(true)

                local unitPanel = self._panel_num
                local unitDot = unitPanel:getChildByName("dot")
                local unitPriceText = unitPanel:getChildByName("num")
                local unitGoldIcon = unitPanel:getChildByName("gold")
                local Panel_supplies = self._panel_supplies
                local buyNumText = Panel_supplies:getChildByName("num_1")
                local totalPanel = Panel_supplies:getChildByName("num_2")
                local buyPriceText = totalPanel:getChildByName("num")
                local totalPriceGoldIcon = totalPanel:getChildByName("gold")
                local btnMinus = Panel_supplies:getChildByName("Button_minus")
                local btnPlus = Panel_supplies:getChildByName("Button_plus")


                if not self._oPriceColor then
                    self._oPriceColor = buyPriceText:getColor()
                end

                self:setCurrencyIcon(unitGoldIcon, goods)
                self:setCurrencyIcon(totalPriceGoldIcon, goods)

                unitPriceText:setString(L("lua_code_text_381") .. goods.price)
                display.setNodesMiddle(unitPanel, unitDot, unitPriceText, unitGoldIcon)

                self:updateGoodsCount(goods)

                btnMinus:addClickEventListener(function(sender)
                    self._model:buyNumMinus()
                    self:updateGoodsCount(goods)
                end)
                btnPlus:addClickEventListener(function(sender)
                    self._model:buyNumPlus(goods)
                    self:updateGoodsCount(goods)
                end)
            end           
        end
        self._panel_condition:setVisible(isMultiCost)
        self._panel_renown:setVisible(not isMultiCost and not isAchieveShop)
        self._panel_achieve:setVisible(not isMultiCost and isAchieveShop)
    else
        if isAchieveShop then
            self:setAchieveShopItemStatus(goods, conditionStatus, siteConditionStr)
        else
            self._panel_lack:setVisible(true)
            
            local iconNode = self._panel_lack:getChildByName("icon")
            local textNode = self._panel_lack:getChildByName("text")
            if iconPath then
                iconNode:loadTexture(iconPath)
            end
            textNode:setString(siteConditionStr)
            textNode:setColor(cc.c3b(255, 105, 85))

            display.setBtnTouchEnable(btnBuy, false)
        end
        self._panel_renown:setVisible(not isAchieveShop)
        self._panel_achieve:setVisible(isAchieveShop)
    end

    -- 是否可以预览
    local bShow, avatarData = false, nil
    local itemData = goods.itemData
    if itemData and itemData.isClassAvatarEnable then
        bShow, avatarData = itemData:isClassAvatarEnable()
    end

    btnBuy:setVisible(true)
    if checkGuild then
        btnBuy:setVisible(false)
    elseif bShow then
        btnLook:setVisible(true)
        btnBuy:setPositionX(originalX)
    else
        btnLook:setVisible(false)
        btnBuy:setPositionX(originalMidX)
    end
    btnBuy:addClickEventListener(function(sender)
        self._model:C2S_BuyGoodsRequest(goods.id, self._model:getBuyNumber(), self._shopId)
    end)
    btnLook:addClickEventListener(function(sender)
        EM:notify("avatarPreview", avatarData)
    end)

end

function ClassRef:updateGoodsCount(goods)
    local isMultiCost = self._model:isMultiCost(goods)
    if isMultiCost then --多个道具刷新按钮状态和道具数量
        local allEnough = true --所有道具数量都足够
        local costData = self._model:transLootToItemData(goods)
        for i, item in ipairs(costData) do
            local name = "icon_"..i
            local iconNode = self._panel_condition:getChildByName(name)
            if iconNode then
                local hasNum = UD:getNumByRenderData(item)
                local isEnough = hasNum >= item.amount
                iconNode:setNumString(hasNum .. "/" .. item.amount)
                if not isEnough then
                    iconNode:setNumberColor(display.COLOR_RED)
                else
                    iconNode:setNumberColor(display.COLOR4_GREEN)
                end
                iconNode:updateNumberSize()
                if not isEnough and allEnough then
                    allEnough = false
                end
            end 
        end
        display.setBtnTouchEnable(self._condition_btnBuy, allEnough)
    else
        local numStr = self._model:getBuyNumber()
        if goods.limitNum > 0 then
            if self._model:getBuyNumber() > goods.leftNum then
                self._model:resetBuyNum()
            end
            numStr = self._model:getBuyNumber() .. "/" .. goods.leftNum
            if goods.leftNum <= 0 then
                numStr = "0/0" -- 显示
            end
        end

        local buyNumText = self._panel_supplies:getChildByName("num_1")
        local totalPanel = self._panel_supplies:getChildByName("num_2")
        local buyPriceText = totalPanel:getChildByName("num")
        local totalPriceGoldIcon = totalPanel:getChildByName("gold")
        local btnMinus = self._panel_supplies:getChildByName("Button_minus")
        local btnPlus = self._panel_supplies:getChildByName("Button_plus")

        buyNumText:setString(L("lua_code_text_57") .. numStr)
        local totalPrice = self._model:getBuyNumber() * goods.price
        local nextPrice = totalPrice + goods.price
        buyPriceText:setString(L("lua_code_text_79") .. totalPrice)
        display.setNodesMiddle(totalPanel, nil, buyPriceText, totalPriceGoldIcon)

        local curGoldNum = self._model:getNumCount(goods)
        if curGoldNum < totalPrice then
            buyPriceText:setColor(cc.c3b(255,0,0))
            display.setBtnTouchEnable(self._btnBuy, false)
        else
            if self._oPriceColor then
                buyPriceText:setColor(self._oPriceColor)
            end
            display.setBtnTouchEnable(self._btnBuy, true)
        end

        display.setBtnTouchEnable(btnPlus,true)
        display.setBtnTouchEnable(btnMinus,true)


        if curGoldNum < goods.price then
            display.setBtnTouchEnable(btnMinus,false)
            display.setBtnTouchEnable(btnPlus,false)
        elseif curGoldNum < nextPrice or (goods.limitNum > 0 and self._model:getBuyNumber() >= goods.leftNum) then
            display.setBtnTouchEnable(btnPlus,false)
        end

        if self._model:getBuyNumber() <= 1 then
            display.setBtnTouchEnable(btnMinus,false)
        end
    end
end

--自己的货币面板更新
function ClassRef:updateGoldInfo()
    local moneyPanel1 = self._ui.Panel_money
    local moneyPanel2 = self._ui.Panel_gold
    moneyPanel1:setVisible(false)
    moneyPanel2:setVisible(false)
    if checknumber(self._shopId) == 72063 then -- 万能徽章商店不显示货币栏
        return
    end
    local function showPanelTips(panel, param)
        local boundingBoxTemp = panel:getBoundingBox()
        local worldPosTemp = panel:getParent():convertToWorldSpace( cc.p( boundingBoxTemp.x, boundingBoxTemp.y ) )
        boundingBoxTemp.x = worldPosTemp.x
        boundingBoxTemp.y = worldPosTemp.y
        param.boundingBox = boundingBoxTemp
        app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params = param})
    end

    local function setPanel(panel, currency)
        panel:setVisible(true)
        local iconNode = panel:getChildByName("icon")
        local numNode = panel:getChildByName("num")
        local szValues = string.split(currency, "|")

        local param = {}
        local icon = nil

        if #szValues == 1 then -- 货币
            local id = tonumber(szValues[1])
            icon = GD:getResIcon(id)
            if id == 90 then -- 成就点
                numNode:setString(UD.player.achievementPoint)
            else
                numNode:setString( UD:getDaibiValue( id ))
            end

            param.itemId = id
            param.tipsType = Enums.TipsType.daibi
        else
            if tonumber(szValues[1]) == Enums.lootType.item then -- 物品
                local id = tonumber(szValues[2])
                local conf = GD:queryItemById(id)
                icon = string.format("%s%s.png", Res.itemPath, conf.icon) -- 设置图标
                numNode:setString(UD:getItemAmountByTid(id))

                param.itemId = id
                param.tipsType = Enums.TipsType.item
            end
        end
        -- 设置图标
        if icon then
            iconNode:loadTexture(icon)
        end
        -- 添加监听事件显示tips
        panel:addTouchEventListener(function(sender, eventType)
            if eventType ~= 2 then return end
            showPanelTips(sender, param)
        end)
    end

    -- 显示商店名称
    local shopConfig = GD:queryShopById(self._shopId)
    self._shopName:setString( L(shopConfig.name) )
    local currencys = string.split(shopConfig.currency, ";")
    if #currencys > 0 then
        setPanel(moneyPanel1, currencys[1])
    end
    if #currencys > 1 then
        setPanel(moneyPanel2, currencys[2])
    end
end

function ClassRef:_onChangeSelectedGoods(index)
    self._sv:setFoucsIndex(index)
    local data = self._sv:getDataByIndex(index)
    if not data then return end
    self:refreshItemStatus(data)
end

function ClassRef:refreshShopList()
    local goods = self._model:getGoodsByShopId(self._shopId)
    local hasData = #goods > 0
    self._sv1:setVisible(false)
    self._sv2:setVisible(false)
    if self._curTabIndex == 1 then
        self._sv = self._sv1
        self._menuBarPanel:setVisible(true)
    else
        self._sv = self._sv2
        self._menuBarPanel:setVisible(false)
    end
    self._sv:setVisible(hasData)
    self._sv:reloadData(goods, {startIndex = 1})
    local flag = self._itemInfo ~= nil
    local renderIdx = 1
    if hasData then 
        if self._itemInfo and goods then
            for i, v in ipairs(goods) do
                local tempType1 = v and protobuf.enum_id("pb_loot.Loot.LootType", v.itemType)
                if (v and self._itemInfo[1] == tempType1 and self._itemInfo[2] == v.itemId) then
                    renderIdx = i
                    self._itemInfo = nil
                    break
                end
            end
        end
        if renderIdx <= #goods then
            self:_onChangeSelectedGoods(renderIdx)
		end     
        if flag then
            self._sv:jumpToIndex(renderIdx)
        end
    end
end

function ClassRef:onQueryGoodsSuccess()
    self._isRequestData = false
    self:refreshShopList()
    self:updateGoldInfo()
end

function ClassRef:onBuyGoodsSuccess(eventName, goods)
    if not goods then return end
    self:updateGoodsCount(goods)
    self:updateShopItem(goods)
    self:updateGoldInfo()
end

return ClassRef
